package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity {

    private Button btn_returnSign1,btn_login1;
    private RadioGroup radio_login;
    private RadioButton radio_yhmlogin,radio_sjhlogin;
    private EditText et_username, et_password;
    private String registeredUsername, registeredPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btn_returnSign1 = findViewById(R.id.btn_returnSign1);
        btn_login1 = findViewById(R.id.btn_login1);
        radio_login = findViewById(R.id.radio_login);
        radio_yhmlogin = findViewById(R.id.radio_yhmlogin);
        radio_sjhlogin = findViewById(R.id.radio_sjhlogin);
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);

        // 接收注册信息
        Intent intent = getIntent();
        if (intent != null) {
            registeredUsername = intent.getStringExtra("username");
            registeredPassword = intent.getStringExtra("password");
            
            // 如果有注册信息，自动填充用户名
            if (registeredUsername != null) {
                et_username.setText(registeredUsername);
            }
        }

//        注册新用户
        btn_returnSign1.setOnClickListener((v)->{
            Intent newIntent = new Intent();
            newIntent.setClass(LoginActivity.this,MainActivity.class);
            startActivity(newIntent);
        });

//       单选框跳转逻辑
        radio_login.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId == R.id.radio_sjhlogin) {
                    startActivity(new Intent(LoginActivity.this, LoginActivity2.class));
                }
            }
        });

//        登陆成功跳转到课程列表页
        btn_login1.setOnClickListener((v)->{
            String username = et_username.getText().toString().trim();
            String password = et_password.getText().toString().trim();

            // 简单的登录验证
            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 验证登录信息
            if ((registeredUsername != null && registeredPassword != null && 
                 username.equals(registeredUsername) && password.equals(registeredPassword)) ||
                (username.equals("admin") && password.equals("123456"))) {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent newIntent = new Intent();
                newIntent.setClass(LoginActivity.this, ListViewActivity.class);
                startActivity(newIntent);
                finish(); // 结束登录页面
            } else {
                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        });

//        传值
//        btnLogin.setOnClickListener((v)->{
//            Intent intent = getIntent();
//            Bundle bundle = intent.getExtras();
//            String name = bundle.getString("用户名");
//            String passwd = bundle.getString("密码");
//            String phone = bundle.getString("电话");
//            String sex = bundle.getString("角色");
//            ArrayList<String> hobbys = bundle.getStringArrayList("hobbys");
//            StringBuffer content = new StringBuffer();
//            for (String s : hobbys){
//                content.append(s);
//                content.append(" ");
//            }
//            Toast.makeText(LoginActivity.this,
//                    "用户名："+name+
//                            "\n密码："+passwd+
//                            "\n电话："+phone+
//                            "\n角色："+sex+
//                            "\n爱好："+content,
//                    Toast.LENGTH_LONG).show();
//        });
    }
}
